package com.tanhua.server.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.tanhua.common.service.SoundUploadService;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.common.vo.PicUploadResult;
import com.tanhua.dubbo.api.SoundApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.pojo.Sound;
import com.tanhua.dubbo.pojo.UserInfo;
import com.tanhua.server.vo.SoundVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class SoundService {

    @DubboReference(version = "1.0.0")
    private SoundApi soundApi;

    @Autowired
    private SoundUploadService soundUploadService;

    //接收语音剩余次数的key
    private final String SOUND_RECEIVE = "RemainingTimes_Receive_";
    //发送语音剩余次数的key
    private final String SOUND_SEND = "RemainingTimes_Send_";

    @DubboReference(version = "1.0.0")
    private UserInfoApi userInfoApi;

    /**
     * 发送语音
     *
     * @param soundFile
     * @return
     */
    public Object saveSound(MultipartFile soundFile) {
        try {
            Sound sound = new Sound();
            sound.setUserId(UserThreadLocal.get());

            //获取剩余次数
            Integer count = this.soundApi.getCount(UserThreadLocal.get(), SOUND_SEND);
            if (count <= 0) {
                return ErrorResult.builder().errCode("500").errMessage("今天的次数已经用完了,明天再来哦~").build();
            }
            //上传语音
            PicUploadResult uploadResult = this.soundUploadService.upload(soundFile);
            sound.setUrl(uploadResult.getName());

            return this.soundApi.saveSound(sound);
        } catch (Exception e) {
            String msg = StrUtil.format("上传语音失败！ soundFile = {}"
                    , soundFile.getOriginalFilename());
            log.error(msg, e);
        }
        return ErrorResult.builder().errCode("500").errMessage("上传语音失败！").build();
    }

    /**
     * 查询语音
     *
     * @return
     */
    public SoundVo querySound() {
        Long userId = UserThreadLocal.get();
        List<Sound> soundList = this.soundApi.querySoundByUserId(userId);

       //将列表中的顺序打乱,随机从列表里拿取一个数据
        Collections.shuffle(soundList);
        Sound sound = soundList.get(RandomUtil.randomInt(soundList.size()));

        //查询用户
        UserInfo userInfo = this.userInfoApi.queryByUserId(sound.getUserId());
        SoundVo soundVo = BeanUtil.toBeanIgnoreError(userInfo, SoundVo.class);
        soundVo.setGender(userInfo.getSex().name().toLowerCase());

        soundVo.setSoundUrl(sound.getUrl());
        soundVo.setRemainingTimes(this.soundApi.getCount(userId, SOUND_RECEIVE));
        return soundVo;
    }
}
